#include "usart.h"
#include "usart_config.h"
#include "ht32f5xxxx_pdma.h"
#include "bluetooth.h"
#include "bldc_communicate.h"
#define _DEVICE_BASE          (device->USART_Base)
#define _DEVICE_BAUD          (device->USART_BaudRate)
#define _DEVICE_STOP_BITS     (device->USART_StopBits)
#define _DEVICE_PARITY_BITS   (device->USART_Parity)
#define _DEVICE_WORDLENGTH    (device->USART_WordLength)
#define _DEVICE_MODE          (device->USART_Mode)

extern USART_Device        RS485_Elbow;
extern USART_Device        RS485_Swing;

void _USART_Init(struct USART_Device * device)
{
    _USART_CreateInitStruct;
    _USART_SET_BAUD          = _DEVICE_BAUD;
    _USART_SET_STOP_BITS     = _DEVICE_STOP_BITS;
    _USART_SET_PARITY_BITS   = _DEVICE_PARITY_BITS;
    _USART_SET_WORDLENGTH    = _DEVICE_WORDLENGTH;
    _USART_SET_MODE          = _DEVICE_MODE;
    _USART_SetInit(device);
}

void _USART_SendData(struct USART_Device * device,unsigned short data)
{
    _USART_SENDDATA(data);
}

void USART_Device_Init(void)
{
    RS485_Elbow.USART_Base = HT_USART0;
    RS485_Elbow.USART_BaudRate = 19200;
    RS485_Elbow.USART_Mode     = USART_MODE_RS485;
    RS485_Elbow.USART_Parity   = USART_PARITY_NO;
    RS485_Elbow.USART_WordLength = USART_WORDLENGTH_8B;
    RS485_Elbow.USART_StopBits = USART_STOPBITS_1;
    _USART_Init(&RS485_Elbow);
    USART_TxCmd(HT_USART0, ENABLE);
    
    RS485_Swing.USART_Base = HT_USART1;
    RS485_Swing.USART_BaudRate = 19200;
    RS485_Swing.USART_Mode     = USART_MODE_RS485;
    RS485_Swing.USART_Parity   = USART_PARITY_NO;
    RS485_Swing.USART_WordLength = USART_WORDLENGTH_8B;
    RS485_Swing.USART_StopBits = USART_STOPBITS_1;
    _USART_Init(&RS485_Swing);
    USART_TxCmd(HT_USART1,ENABLE);

    DX2003.USART_Base       = HT_UART1;
    DX2003.USART_BaudRate   = 115200;
    DX2003.USART_Mode       = USART_MODE_NORMAL;
    DX2003.USART_Parity     = USART_PARITY_NO;
    DX2003.USART_WordLength = USART_WORDLENGTH_8B;
    DX2003.USART_StopBits   = USART_STOPBITS_1;
    _USART_Init(&DX2003);
    USART_TxCmd(HT_UART1,ENABLE);
    USART_RxCmd(HT_UART1,ENABLE);
    USART_IntConfig(HT_UART1,USART_INT_RXDR,ENABLE);
    USART_PDMACmd(HT_UART1, USART_PDMAREQ_TX, ENABLE);
    {   //PDMA Connect to UART1
        USART_TxCmd(DX2003.USART_Base,ENABLE);
        HT_PDMA->PDMACH5.CR   = SRC_ADR_LIN_INC | DST_ADR_FIX | M_PRIO | WIDTH_8BIT;
        HT_PDMA->PDMACH5.DADR = (unsigned int)(&(HT_UART1->DR));
    }

    CAN.USART_Base          = HT_UART0;
    CAN.USART_BaudRate      = 115200;
    CAN.USART_Mode          = USART_MODE_NORMAL;
    CAN.USART_Parity        = USART_PARITY_NO;
    CAN.USART_WordLength    = USART_WORDLENGTH_8B;
    CAN.USART_StopBits      = USART_STOPBITS_1;
    _USART_Init(&CAN);
    USART_TxCmd(HT_UART0, ENABLE);
    USART_RxCmd(HT_UART0, ENABLE);
    USART_PDMACmd(HT_UART0, USART_PDMAREQ_RX, ENABLE);

    //PDMA Connect to UART0
    HT_PDMA->PDMACH2.CR   = SRC_ADR_FIX | DST_ADR_LIN_INC | M_PRIO | WIDTH_8BIT | AUTO_RELOAD;
    HT_PDMA->PDMACH2.SADR = (unsigned int)(&HT_UART0->DR);
    HT_PDMA->PDMACH2.DADR = (unsigned int)(CANRecieveBuffer);
    HT_PDMA->PDMACH2.TSR  = ((7<<16) | 0x01);
    PDMA_IntConfig(PDMA_CH2, PDMA_INT_TC | PDMA_INT_GE, ENABLE);
    PDMA_EnaCmd(PDMA_CH2, ENABLE);
}

